﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Collections.Specialized;
using System.Configuration.Provider;

namespace PB.FBConnect
{
    public class FBMembershipProvider : MembershipProvider
    {
        #region Constants
        private const string Not_Supported_Error_Msg = "{0} is not supported in Facebook Membership Provider";
        private const string Not_Implemented_Error_Msg = "{0} is not implemented in Facebook Membership Provider under current scope";
        #endregion

        #region Overridden Members

        #region NotSupported Members

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "FindUsersByEmail"));
        }
        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "FindUsersByName"));
        }
        public override int GetNumberOfUsersOnline()
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "GetNumberOfUsersOnline"));
        }
        public override string ApplicationName
        {
            get
            {
                return "FBMembership-Demo";
            }
            set
            {
                throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "ApplicationName-Set"));
            }
        }
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "ChangePassword"));
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "ChangePasswordQuestionAndAnswer"));
        }



        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "DeleteUser"));
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "GetPassword"));
        }
        public override int MinRequiredPasswordLength
        {
            get
            {
                return 0;
            }
        }
        public override int PasswordAttemptWindow
        {
            get
            {
                return 0;
            }
        }
        public override MembershipPasswordFormat PasswordFormat
        {
            get
            {
                return MembershipPasswordFormat.Clear;
            }
        }
        public override string PasswordStrengthRegularExpression
        {
            get
            {
                return string.Empty;
            }
        }
        public override int MaxInvalidPasswordAttempts
        {
            get
            {
                return -1;
            }
        }
        public override int MinRequiredNonAlphanumericCharacters
        {
            get
            {
                return 0;
            }
        }
        public override string ResetPassword(string username, string answer)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "ResetPassword"));
        }
        public override bool UnlockUser(string userName)
        {
            long fbUserId = -1;
            if (!string.IsNullOrEmpty(userName)
                   && long.TryParse(userName, out fbUserId)
                   && fbUserId > 0)
            {
                return FBMembership.UnlockUser(fbUserId);
            }
            return false;
        }

        public override void UpdateUser(MembershipUser user)
        {
            if (user != null)
            {
                FBUser fbUser = user as FBUser;
                if(fbUser != null && fbUser.CustomUser != null && fbUser.CustomUser.FacebookUserId > 0)
                {
                    FBMembership.UpdateUser(fbUser.CustomUser);
                }
            }
        }
        public override string GetUserNameByEmail(string email)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "GetUserNameByEmail"));
        }
        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
           return FBMembership.GetAllUsersCollection(pageIndex, pageSize, out totalRecords);
        }
        #endregion


        public override bool EnablePasswordReset
        {
            get
            {
                return false;
            }
        }

        public override bool EnablePasswordRetrieval
        {
            get
            {
                return false;
            }
        }
        
        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return false; }
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "CreateUser"));
        }
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            FBUser user = null;
            long fbUserId = -1;
            if(!string.IsNullOrEmpty(username) 
                    &&  long.TryParse(username, out fbUserId)
                    &&  fbUserId > 0 )
            {
                user =  FBMembership.GetFBUser(fbUserId);
            }

            return user;
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            return GetUser(providerUserKey.ToString(), userIsOnline);
        }

        public override bool ValidateUser(string username, string password)
        {
            return (new FBSession()).IsConnected();
        }
        #endregion

        #region Override Members from System.Configuration.Provider.ProviderBase

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
        }
        #endregion

    }
}
